package com.yundian.cloudshop.action;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.opennice.tools.Tools;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.yundian.filekind.dao.impl.FilekindDAO;
import com.yundian.shop.service.impl.ShopService;
import com.yundian.shopprice.dao.impl.ShoppriceDAO;
import com.yundian.vo.Member;
import com.yundian.vo.Shop;
import com.yundian.vo.Shopprice;

public class CloudShopAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	String result;
	ShopService shopService;
	String shopShowName;
	String shopName;
	String password;
	String email;
	/** 上传文件的属性 */
	File logo;	//上传的文件
    String logoContentType;	//上传文件的类型
    String logoFileName;		//上传的文件名
    
	String address;
	String shopLocation;
	String telephone;
	String time;
	int kind;
	String desc;
	Shop shop;
   List	listShops;
   Member member;
   List shopprices;
   ShoppriceDAO shoppriceDAO;
   List filekinds;
   FilekindDAO filekindDAO;
   public String showShop() throws Exception{
	     listShops=  shopService.browseShop();
	     member=(Member) ServletActionContext.getRequest().getSession().getAttribute("member");
			
		return "showShop";
	}
   public String registerShop() throws IOException{
	   if(shopShowName!=null&&shopName!=null&&password!=null&&email!=null){
		  shopLocation="shopLocation";
		  String logoname = "";
		  if (getLogoFileName()!=null && getLogoFileName().trim().length()>0){
				String tempfilename = Tools.getRndFilename()+Tools.getFileExtName(getLogoFileName());
				System.out.println(ServletActionContext.getRequest());
				String filename = ServletActionContext.getRequest().getRealPath("/dmrc/logo").replaceAll("\\\\", "/")+"/"+tempfilename;
				FileOutputStream fos = new FileOutputStream(filename);
				FileInputStream fis = new FileInputStream(getLogo());
				byte[] buf = new byte[1024];
				int len = 0;
				while((len=fis.read(buf))>0){
					fos.write(buf,0,len);
				}
				if (fis!=null)fis.close();
				if (fos!=null)fos.close();
				logoname=tempfilename;
			}
		 shop= shopService.registerShop(shopShowName, shopName, password, email, logoname, address, shopLocation, telephone, time, kind, desc);
		 ServletActionContext.getRequest().getSession().setAttribute("shop", shop);
		 shopprices=shoppriceDAO.browseShoppriceByshopId(shop.getShopId());
		 filekinds=filekindDAO.browseFilekind();
	   }else
		   return "login";
	   
	   
	   return "homeShop";
   }
	public String getResult() {
		return result;
	}
	public void setResult(String result) {
		this.result = result;
	}
	public List getShopprices() {
		return shopprices;
	}
	public void setShopprices(List shopprices) {
		this.shopprices = shopprices;
	}
	public ShoppriceDAO getShoppriceDAO() {
		return shoppriceDAO;
	}
	public void setShoppriceDAO(ShoppriceDAO shoppriceDAO) {
		this.shoppriceDAO = shoppriceDAO;
	}
	public List getFilekinds() {
		return filekinds;
	}
	public void setFilekinds(List filekinds) {
		this.filekinds = filekinds;
	}
	public FilekindDAO getFilekindDAO() {
		return filekindDAO;
	}
	public void setFilekindDAO(FilekindDAO filekindDAO) {
		this.filekindDAO = filekindDAO;
	}
	public Member getMember() {
		return member;
	}
	public void setMember(Member member) {
		this.member = member;
	}
	public List getListShops() {
		return listShops;
	}
	public void setListShops(List listShops) {
		this.listShops = listShops;
	}
	public String getLogoContentType() {
		return logoContentType;
	}
	public void setLogoContentType(String logoContentType) {
		this.logoContentType = logoContentType;
	}
	public String getLogoFileName() {
		return logoFileName;
	}
	public void setLogoFileName(String logoFileName) {
		this.logoFileName = logoFileName;
	}
	public void setLogo(File logo) {
		this.logo = logo;
	}
	public ShopService getShopService() {
		return shopService;
	}
	public void setShopService(ShopService shopService) {
		this.shopService = shopService;
	}
	public String getShopShowName() {
		return shopShowName;
	}
	public void setShopShowName(String shopShowName) {
		this.shopShowName = shopShowName;
	}
	public String getShopName() {
		return shopName;
	}
	public void setShopName(String shopName) {
		this.shopName = shopName;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getShopLocation() {
		return shopLocation;
	}
	public void setShopLocation(String shopLocation) {
		this.shopLocation = shopLocation;
	}
	public String getTelephone() {
		return telephone;
	}
	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}
	public String getTime() {
		return time;
	}
	public void setTime(String time) {
		this.time = time;
	}
	public int getKind() {
		return kind;
	}
	public void setKind(int kind) {
		this.kind = kind;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
	public Shop getShop() {
		return shop;
	}
	public void setShop(Shop shop) {
		this.shop = shop;
	}
	public static long getSerialVersionUID() {
		return serialVersionUID;
	}
	public File getLogo() {
		return logo;
	}
	
	
}
